-
Notifications
You must be signed in to change notification settings - Fork 15
/
Sort Linked List.py
103 lines (84 loc) · 2.12 KB
/
Sort Linked List.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Github: Shantanugupta1118
# DAY 25 of DAY 100
# Sort Linked List - InterviewBit/Google
# https://www.interviewbit.com/problems/sort-list/
from collections import deque
class Node:
def __init__(self, data):
self.data = data
self. next = None
class Solution:
def __init__(self):
self.head = None
def push(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
curr = self.head
while curr.next:
curr = curr.next
curr.next = new_node
# LIST METHOD
'''
def sorting(self):
value = []
temp = self.head
while self.head:
value.append(self.head.data)
self.head = self.head.next
value.sort()
value = deque(value)
self.head = temp
while temp:
temp.data = value.popleft()
temp = temp.next
'''
# Direct LinkedList Sort
def sorting(self):
if self.head is None:
return
else:
curr = self.head
idx = None
while curr != None:
idx = curr.next
while idx != None:
if curr.data > idx.data:
curr.data, idx.data = idx.data, curr.data
idx = idx.next
curr = curr.next
def display(self):
if self.head is None:
return None
else:
curr = self.head
while curr:
print(curr.data, end= ' -> ')
curr = curr.next
print("NULL\n\n")
# TEST CASES-------------------------------
ll = Solution()
arr = [3,5,2,1,4,7]
for i in arr:
ll.push(i)
print("-------- ARRAY 1 --------")
ll.display()
ll.sorting()
ll.display()
ll2 = Solution()
arr2 = [5,2,9,6,7,4,1]
for i in arr2:
ll2.push(i)
print("-------- ARRAY 2 --------")
ll2.display()
ll2.sorting()
ll2.display()
ll3 = Solution()
arr3 = [2,3,4,5,6,7]
for i in arr3:
ll3.push(i)
print("-------- ARRAY 3 --------")
ll3.display()
ll3.sorting()
ll3.display()